package com.leetcode.algorithm.y22.m07;

import java.util.HashMap;
import java.util.Map;

/**
 * 205. 同构字符串
 * 
 * https://leetcode.cn/problems/isomorphic-strings/
 * 
 * @author jie.deng
 *
 */
class Question0205Solution01 {

	public boolean isIsomorphic(String s, String t) {
		Map<Character, Character> map = new HashMap<>();
		Map<Character, Character> map2 = new HashMap<>();
		int lenS = s.length();
		int lenT = t.length();
		if (lenS != lenT) {
			return false;
		}
		int idx = 0;
		while (idx < lenS) {
			char chS = s.charAt(idx);
			char chT = t.charAt(idx);
			if (!map.containsKey(chS)) {
				if (map2.containsKey(chT)) {
					// 不同字符不能映射到同一个字符上
					return false;
				}
				map.put(chS, chT);
				map2.put(chT, chS);
			} else {
				if (chT != map.get(chS)) {
					// 相同字符只能映射到同一个字符上
					return false;
				}
			}
			idx++;
		}
		return true;
	}

}